import { createRouter, createWebHashHistory } from "vue-router";
import easyapi from '../api'

const isLogin = async function (to, from) {
    let token = sessionStorage.getItem('token') || localStorage.getItem('token')
    //验证token是否有效
    let result = await easyapi.get('api/checkToken')
    if (!token || result.code != 200) {
        return { path: "/login" };
    }
}



const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        { path: "/", redirect: "/login" },
        {
            path: "/main",
            component: () => import('../views/main.vue'),
            children: [
                { path: '/member', component: () => import("../views/member.vue") },
                { path: '/trainer', component: () => import("../views/trainer.vue") },
                { path: '/private', component: () => import("../views/private.vue") },
                { path: '/public', component: () => import("../views/public.vue") },
                { path: '/register', component: () => import("../views/register.vue") },
                { path: '/membertype', component: () => import("../views/membertype.vue") },
                { path: '/order', component: () => import("../views/order.vue") },
                { path: '/privateclass', component: () => import("../views/privateclass.vue") },
                { path: '/user', component: () => import("../views/user.vue") },
                { path: '/charts', component: () => import("../views/charts.vue") }
            ],
            beforeEnter:isLogin
        },
        {
            path: "/login",
            component: () => import('../views/login.vue')
        }


    ]
})

export default router;